package com.ng.desktop.view.dieta;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;

import com.ng.framework.view.desktop.NGGUIUtils;

public class DietaPanel extends JPanel implements ActionListener, CopiarListener {
	

	private static final long serialVersionUID = -1299966661022864525L;

	private String paciente;
	private Integer duracion;
	private Integer calorias;
	private JTabbedPane tabbedPaneDias;
	private JButton buttonImprimir;
	
	public DietaPanel() {
		this.buildGui();
	}
	
	private void buildGui() {
		this.setLayout(new GridBagLayout());
		
		this.add(this.getTabbedPaneDias(), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 0, 0), 0, 0));
		this.add(this.getButtonImprimir(), new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
	}
	
	private JTabbedPane getTabbedPaneDias() {
		if(tabbedPaneDias == null) {
			this.tabbedPaneDias = new JTabbedPane();
		}
		return tabbedPaneDias;
	}
	
	private JButton getButtonImprimir() {
		if(buttonImprimir == null) {
			this.buttonImprimir = NGGUIUtils.createIconButton("icons/printer.png", this);
		}
		return buttonImprimir;
	}

	public void generarDieta(Integer duracion, HashMap<Comida, HashMap<Long, Double>> porcionesComida) {
		this.duracion = duracion;
		
		int tabCount = this.getTabbedPaneDias().getTabCount();
		if(tabCount > duracion) {
			int tabsToRemove = tabCount - duracion;
			
			for(int i = 0; i < tabsToRemove; i++) {
				this.getTabbedPaneDias().remove(this.getTabbedPaneDias().getTabCount() - 1);				
			}
		} else if(tabCount < duracion) {
			int tabsToAdd = duracion - tabCount;
			
			for(int i = 1; i <= tabsToAdd; i++) {
				DiaPanel diaPanel = new DiaPanel(i, porcionesComida);
				diaPanel.addCopiarListener(this);
				this.getTabbedPaneDias().addTab("Día " + (tabCount + i), diaPanel);
			}
		}
	}
	
	private List<DiaDieta> getDiasDieta() {
		List<DiaDieta> diasDieta = new ArrayList<DiaDieta>();
		
		for(int i = 0; i < this.getTabbedPaneDias().getTabCount(); i++) {
			Component component = this.getTabbedPaneDias().getComponentAt(i);
			if(component instanceof DiaPanel) {
				diasDieta.add(((DiaPanel)component).getDiaDieta());
			}
		}
		
		return diasDieta;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.getButtonImprimir()) {
			Map<String, Object> parameters = new HashMap<String, Object>();
			
			parameters.put("paciente", this.paciente);
			parameters.put("calorias", this.calorias);
			parameters.put("duracion", this.duracion);
			
			try {
				JasperReport report = JasperCompileManager.compileReport(ClassLoader.getSystemClassLoader().getResourceAsStream("reports/report1.jrxml"));
				JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(this.getDiasDieta());
				JasperPrint jasperPrint = JasperFillManager.fillReport(
				            report, parameters, dataSource);
				JasperViewer.viewReport(jasperPrint, false);
			} catch (JRException e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(this, "Ha ocurrido un error al generar el reporte. Por favor, comuníquese con el desarrollador de la aplicacón.", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	public void setPaciente(String paciente) {
		this.paciente = paciente;
	}
	
	public void setCalorias(Integer calorias) {
		this.calorias = calorias;
	}
	
	@Override
	public void copiar(CopiarEvent evt) {
		DiaPanel diaPanelDestino = (DiaPanel)evt.getSource();
		
		Integer diaOrigen = evt.getDia();
		if(!diaPanelDestino.getDiaDieta().getDia().equals(diaOrigen)) {
			for(int i = 0; i < this.getTabbedPaneDias().getTabCount(); i++) {
				Component component = this.getTabbedPaneDias().getComponentAt(i);
				if(component instanceof DiaPanel) {
					DiaPanel diaPanel = (DiaPanel)component;
					
					if(diaPanel.getDia().equals(evt.getDia())) {
						
						for(Comida comida : evt.getComidas()) {
							diaPanelDestino.setCantidadAliementos(comida, diaPanel.getCantidadAlimentos(comida));
							diaPanelDestino.setRecetaComida(comida, diaPanel.getRecetaComida(comida));
						}
					}
				}
			}
		}
	}

}
